
package com.timber.util;

import java.util.*;
import java.text.*;
import pv.jfcx.*;

/**
 * static methods to convert Object parameter to appropriate type.
 * parameter Object is of the type itself or String
 * methods return an Object or a primitive value.
 * return null or 0 when appropriate
 */
public class TypeUtils
{

  /**
   * return primitive long value
   */
  public static long getLongValue( Object o ) throws Exception
  {
    if( o != null )
      {
	if( o instanceof Long )
	  return ((Long)o).longValue();
	else
	  if( o instanceof String && ((String)o).length() > 0 )
	    return Long.parseLong( (String)o );
      }

    return 0;
  }

  /**
   *	convert to Long
   */
  public static Long getLong( Object o ) throws Exception
  {
    if( o != null )
    {
      if( o instanceof Long )
	return ((Long)o);
      else
	if( o instanceof String && ((String)o).length() > 0 )
	  return new Long( (String)o );
    }

    return null;
  }

  /**
   * return primitive double value
   */
  public static double getDoubleValue( Object o ) throws Exception
  {
    if( o != null )
    {
      if( o instanceof Double )
	return ((Double)o).doubleValue();
      else
	if( o instanceof String && ((String)o).length() > 0 )
	  return Double.parseDouble( (String)o );
    }

    return 0.0;
  }

  /**
   *	convert to Double
   */
  public static Double getDouble( Object o ) throws Exception
  {
    if( o != null )
    {
      if( o instanceof Double )
	return ((Double)o);
      else
	if( o instanceof String )
	  return new Double( (String)o );
    }

    return null;
  }

  /**
   * return primitive float value
   */
  public static float getFloatValue( Object o ) throws Exception
  {
    if( o != null )
    {
      if( o instanceof Float )
	return ((Long)o).floatValue();
      else
	if( o instanceof String && ((String)o).length() > 0 )
	  return Float.parseFloat( (String)o );
    }

    return 0;
  }

  /**
   *	convert to Float
   */
  public static Float getFloat( Object o ) throws Exception
  {
    if( o != null )
    {
      if( o instanceof Float )
	return ((Float)o);
      else
	if( o instanceof String )
	  return new Float( (String)o );
    }

    return null;
  }

  /**
   * return primitive int value
   */
  public static int getIntValue( Object o ) throws Exception
  {
    if( o != null )
    {
      if( o instanceof Integer )
	return ((Integer)o).intValue();
      else
	if( o instanceof String && ((String)o).length() > 0 )
	  return Integer.parseInt( (String)o );
    }

    return 0;
  }

  /**
   *	convert to Integer
   */
  public static Integer getInteger( Object o ) throws Exception
  {
    if( o != null )
    {
      if( o instanceof Integer )
	return ((Integer)o);
      else
	if( o instanceof String && ((String)o).length() > 0 )
	  return new Integer( (String)o );
    }

    return null;
  }

  /**
   *	convert to Date
   */
  public static java.util.Date getDate( Object o ) throws Exception
  {
    if( o != null )
    {
      if ( o instanceof java.util.Date )
	return ( java.util.Date )o;
      else
	if ( o instanceof String && ((String)o).length() > 0 )
	  return ( DateFormat.getDateInstance(java.text.DateFormat.SHORT) ).parse( ( String )o );
    }

    return null;
  }

  public static int sqlType2JPV(int sqlType)
  {
    switch( sqlType )
    {
      case java.sql.Types.DATE:
      case java.sql.Types.TIME:
      case java.sql.Types.TIMESTAMP:
	return JPVTable.DATE;

      case java.sql.Types.DOUBLE:
      case java.sql.Types.FLOAT:
      case java.sql.Types.DECIMAL:
      case java.sql.Types.NUMERIC:
	return JPVTable.DOUBLE;

      case java.sql.Types.INTEGER:
      case java.sql.Types.SMALLINT:
      case java.sql.Types.TINYINT:
	return JPVTable.LONG;

      case java.sql.Types.CHAR:
	return JPVTable.CHARACTER;

	//case java.sql.Types.BIT:
      case JPVTable.BOOLEAN:
	return JPVTable.BOOLEAN;

      case java.sql.Types.JAVA_OBJECT:
	return JPVTable.COMBO;

      case java.sql.Types.VARCHAR:
      case java.sql.Types.NULL:
      default:
	return JPVTable.TEXT_NULL;
    }
  }
}
